//
//  BDVIPTableViewCell.m
//  BadBoy
//
//  Created by wen on 2016/12/26.
//  Copyright © 2016年 niusaibing. All rights reserved.
//

#import "BDVIPTableViewCell.h"
#import "BDUserModel.h"

@interface BDVIPTableViewCell ()
{
    NSString *botLabelString;
}
@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIImageView *VipImageview;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *vipNameLabel;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UILabel *vipLabel;
@property (nonatomic, strong) YYLabel *bottomLab;
@property (nonatomic, strong) UIButton *button;

@property (nonatomic, copy) void(^buttonBlock)();

@end

@implementation BDVIPTableViewCell

- (void)configeWithModel:(id)model buttonAction:(void(^)())action{
    NSInteger index = [model[@"product_id"] integerValue];
    NSInteger imageIndex = index > 10 ? index - 13 : index;
    self.VipImageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"VIP%@",@(imageIndex)]];
    self.vipLabel.text = ASLocalizedString(@"VIP 特权");
    
    self.vipNameLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@ %@天"), model[@"title"], @([model[@"validity"] integerValue] / 3600 / 24)];
//    if ([model[@"product_id"] integerValue] == 1) {
//        self.moneyLabel.text = [NSString stringWithFormat:ASLocalizedString(@"238 元")];
//    }else if ([model[@"product_id"] integerValue] == 2) {
//        self.moneyLabel.text = [NSString stringWithFormat:ASLocalizedString(@"488 元")];
//    }
    
    self.moneyLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@ 元"), @([model[@"money"] floatValue])];
    botLabelString = model[@"content"][@"content"];
    
    BDUserModel *user = [BDUserModel getCurrentUser];
    if (user.vip_level == 0) {
        self.button.backgroundColor = [UIColor colorWithRed:76/255.0 green:122/255.0 blue:173/255.0 alpha:1.0f];
        [self.button setTitle:ASLocalizedString(@"开通")forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.button.userInteractionEnabled = YES;
    }else {
        self.button.backgroundColor = kBDColorGray;
        [self.button setTitle:ASLocalizedString(@"已开通")forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.button.userInteractionEnabled = NO;
    }
//    self.button.tag = indexPath.row;
//    if (indexPath.row == 0) {
//        self.vipNameLabel.text = ASLocalizedString(@"VIP1 白银VIP");
//        self.moneyLabel.text = ASLocalizedString(@"6个月160元");
//        botLabelString = ASLocalizedString(@"免费查看所有付费的内容。");
//    }else{
//        self.vipNameLabel.text = ASLocalizedString(@"VIP1 黄金VIP");
//        self.moneyLabel.text = ASLocalizedString(@"年费300元");
//        botLabelString = ASLocalizedString(@"1.开通成白银VIP，就可以看到所有特殊内容。\n2.开通成黄金VIP，就可享受礼品订阅");
//    }
    self.buttonBlock = action;
}

- (void)layoutSubviews{
    
    self.bkView.size = CGSizeMake(self.width - 20, self.height - 20);
    self.bkView.center = CGPointMake(self.width / 2.0, self.height / 2.0);
    self.bkView.backgroundColor = [UIColor whiteColor];
    self.bkView.layer.cornerRadius = 5;
    self.bkView.layer.masksToBounds = YES;
    
    self.vipNameLabel.textColor = KBDNameColor;
    self.vipNameLabel.font = [UIFont systemFontOfSize:15];
    [self.vipNameLabel sizeToFit];
    self.vipNameLabel.top = 15;
    
    self.moneyLabel.font = [UIFont systemFontOfSize:13];
    self.moneyLabel.textColor = [UIColor colorWithRed:255/255.0 green:147/255.0 blue:0.0 alpha:1.0f];
    [self.moneyLabel sizeToFit];
    self.moneyLabel.top = self.vipNameLabel.bottom + 5;
    
    self.VipImageview.height = self.moneyLabel.bottom - self.vipNameLabel.top;
    self.VipImageview.width = self.VipImageview.image.size.width / 1.0 / self.VipImageview.image.size.height * self.VipImageview.height;
    self.VipImageview.top = 15;
    self.VipImageview.left = 15;
    
    self.lineView.size = CGSizeMake(1, self.VipImageview.height);
    self.lineView.left =self.VipImageview.right + 15;
    self.lineView.top = 15;
    self.lineView.backgroundColor = kBDTextBlackColorGray;
    
    self.vipNameLabel.left = self.lineView.right + 15;
    self.moneyLabel.left = self.vipNameLabel.left;
    
    self.vipLabel.textColor = [UIColor colorWithRed:77/255.0 green:122/255.0 blue:173/255.0 alpha:1.0f];
    self.vipLabel.font = [UIFont systemFontOfSize:13];
    [self.vipLabel sizeToFit];
    self.vipLabel.top = self.VipImageview.bottom + 30;
    self.vipLabel.left = self.VipImageview.left;
    
    
    self.bottomLab.textColor = [UIColor colorWithRed:118/255.0 green:118/255.0 blue:118/255.0 alpha:1.0f];
    self.bottomLab.font = [UIFont systemFontOfSize:13];
    self.bottomLab.numberOfLines = 0;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[botLabelString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    attributedString.lineSpacing = 4;
    self.bottomLab.attributedText = attributedString;
    [self.bottomLab sizeToFit];
    self.bottomLab.left = 15;
    self.bottomLab.top = self.vipLabel.bottom + 10;
    
    self.button.layer.cornerRadius = 5;
    self.button.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(buttonPress) forControlEvents:UIControlEventTouchUpInside];
    self.button.size = CGSizeMake(60, 30);
    self.button.right = self.bkView.width - 15;
    self.button.centerY = self.bkView.height / 2.0;
}

- (void)buttonPress{
    self.button.userInteractionEnabled = NO;
    [self performSelector:@selector(refreshUserEnable) withObject:nil afterDelay:1.0];
    if (self.buttonBlock) {
        self.buttonBlock();
    }
}

- (void)refreshUserEnable{
    self.button.userInteractionEnabled = YES;
}

- (UIView *)bkView{
    if (!_bkView) {
        self.bkView = [[UIView alloc] init];
        [self.contentView addSubview:_bkView];
    }
    return _bkView;
}

-(UIImageView *)VipImageview{
    if (!_VipImageview) {
        _VipImageview = [[UIImageView alloc]init];
        [self.bkView addSubview:_VipImageview];
    }
    return _VipImageview;
}
-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        [self.bkView addSubview:_lineView];
    }
    return _lineView;
}
-(UILabel *)vipNameLabel{
    if (!_vipNameLabel) {
        _vipNameLabel = [[UILabel alloc]init];
        [self.bkView addSubview:_vipNameLabel];
    }
    return _vipNameLabel;
}
-(UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel = [[UILabel alloc]init];
        [self.bkView addSubview:_moneyLabel];
    }
    return _moneyLabel;
}

- (UILabel *)vipLabel{
    if (!_vipLabel) {
        _vipLabel = [[UILabel alloc]init];
        [self.bkView addSubview:_vipLabel];
    }
    return _vipLabel;
}
-(YYLabel *)bottomLab{
    if (!_bottomLab) {
        _bottomLab = [[YYLabel alloc]init];
        [self.bkView addSubview:_bottomLab];
    }
    return _bottomLab;
}
-(UIButton *)button{
    if (!_button) {
        _button = [[UIButton alloc]init];
        [self.bkView addSubview:_button];
    }
    return _button;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
